﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Ajax.Utilities;
using Newtonsoft.Json.Schema;
using ZhongheCms.Models;
namespace ZhongheCms.Controllers
{
    public class HomeDataController : Controller
    {


        public ActionResult Index(int id = 0, string m = "", string f = "", string p = "", string fid = "", string r = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry = GswTblHomeDatum.Fetch("select top 10 *  from GswTblHomeData where ParentId=" + GswTblUser.CurrentUser.CustomerId + " order by id desc");
            ViewBag.scode = "1";
            //var s = "1515187dz";
            //var  pwdSalt = GsnHelper.EncryptPwd.GenerateSalt();
            //var  pwdHash = GsnHelper.EncryptPwd.EncodePassword(s, pwdSalt);
            return View(qry);
        }

        public ActionResult GenerationData(int id = 0, string m = "", string f = "", string p = "", string fid = "", string r = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.scode = "0";
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            try
            {
                GswTblHomeContent.Update("set UpdateFlag=1");
                GswTblHomeJobInfo.Update("set UpdateFlag=1");
               
                //精彩一刻
                //var sql="select top 8 * from GswTblContent where FunctionId=84 and CustomerId=19 and NewFlag=1 order by id desc";
                var sql = "  where FunctionId=84 and CustomerId=19 and NewFlag=1 order by id desc";
               FillContentData(sql,8);
                //学员风采
               sql = "  where FunctionId=102 and CustomerId=19 and NewFlag=1 order by id desc";
                FillContentData(sql,8);
                //主题活动
                sql = "  where FunctionId=85 and CustomerId=19 and NewFlag=1 order by id desc";
                FillContentData(sql,4);
                //就业资讯
                sql = " where FunctionId=83 and CustomerId=19 order by id desc";
                    
                FillContentData(sql,6);
                //培训右侧文字
                sql = "  where FunctionId=82 and CustomerId=19  and NewFlag=0 and ReFlag=0 order by id desc";
                FillContentData(sql,6);
                //培训顶部图片
                sql = "  where FunctionId=82 and CustomerId=19 and NewFlag=1 order by id desc"; 
                FillContentData(sql,4);
                //培训图片6
                sql = "  where FunctionId=82 and CustomerId=19 and ReFlag=1 order by id desc";
                FillContentData(sql,6);
                //公告通知
                sql = "  where FunctionId=79 and CustomerId=19 order by id desc";
                FillContentData(sql,20);
                //轮播图片
                sql = "  where FunctionId=81 and CustomerId=19 order by id desc";
                FillContentData(sql,4);
                //校园分享
                sql = " where FunctionId=90 and CustomerId=19 and NewFlag=1 order by id desc";
                FillContentData(sql,8);
                //友情链接
                FillFrdLinkData();
               // sql = "  where  ParentId=19 and HotFlag=1 and JobState=1 order by id desc";
               // FillJobData(sql,8);
                sql = "  where  ParentId=19 and ReFlag=1 and JobState=1 order by id desc";
                FillJobData(sql, 4);

                GswTblHomeContent.Delete(" where UpdateFlag=1");
                GswTblHomeJobInfo.Delete(" where UpdateFlag=1");
                var homedata = new GswTblHomeDatum
                {
                    LastDate = DateTime.Today,
                    UserName = GswTblUser.CurrentUser.TrueName,
                    ParentId = GswTblUser.CurrentUser.CustomerId,
                    LastTime = DateTime.Now.ToShortTimeString()
                };
                homedata.Insert();

            }
            catch (Exception e)
            {

                ModelState.AddModelError(string.Empty, e.Message);
                ViewBag.scode = "1";
            }
            //HappyTime = GswTblContent.Fetch(
            //        "select top 8 * from GswTblContent where FunctionId=84 and CustomerId=19 and NewFlag=1 order by id desc"),
            //MemberNews = GswTblContent.Fetch(
            //        "select top 8 * from GswTblContent where FunctionId=102 and CustomerId=19 and NewFlag=1 order by id desc"),
            //ThemedEvents = GswTblContent.Fetch(
            //        "select top 4 * from GswTblContent where FunctionId=85 and CustomerId=19 and NewFlag=1 order by id desc"),

            //Employed = GswTblContent.Fetch(
            //        "select top 6 * from GswTblContent where FunctionId=83 and CustomerId=19 and NewFlag=1 order by id desc"),
            //ClassList = GswTblContent.Fetch(
            //        "select top 6 * from GswTblContent where FunctionId=82 and CustomerId=19  order by id desc"),
            //ReClassList = GswTblContent.Fetch(
            //        "select top 4 * from GswTblContent where FunctionId=82 and CustomerId=19 and ReFlag=1 order by id desc"),
            //HotClassList = GswTblContent.Fetch(
            //        "select top 6 * from GswTblContent where FunctionId=82 and CustomerId=19 and NewFlag=1 order by id desc"),
            //NoticeList =
            //  GswTblContent.Fetch(
            //      "select top 8 * from GswTblContent where FunctionId=79 and CustomerId=19 order by id desc"),
            //HotNews =
            //    GswTblContent.Fetch(
            //        "select top 4 * from GswTblContent where FunctionId=81 and CustomerId=19 order by id desc"),
            //HotJobList = GswTblJobInfo.Fetch("select top 10 * from GswTblJobInfo where  ParentId=19 and HotFlag=1 and JobState=1 order by id desc"),
            //ReJobList = GswTblJobInfo.Fetch("select top 4 * from GswTblJobInfo where  ParentId=19 and ReFlag=1 and JobState=1 order by id desc"),
            //SchoolShare = GswTblContent.Fetch(
            //        "select top 8 * from GswTblContent where FunctionId=90 and CustomerId=19 and NewFlag=1 order by id desc"),
            var qry = GswTblHomeDatum.Fetch("where ParentId=" + GswTblUser.CurrentUser.CustomerId + " order by id desc");
            return View("Index",qry);
        }

        private void FillFrdLinkData()
        {
            var strsql = "insert into GswTblHomeContent(Id, CreateDate, ContentTitle, ContentBrief, ContentDesc, Author, ContentPic, ContentThumb, CustomerId,ContentTypeId, FunctionId, NewFlag, ContentNo, RelateId, FileDesc, NewsY, NewsM, ReFlag)";
            strsql += " select  Id, CreateDate, ContentTitle, ContentBrief, ContentDesc, Author, ContentPic, ContentThumb, CustomerId,ContentTypeId, FunctionId, NewFlag, ContentNo, RelateId, FileDesc, NewsY, NewsM, ReFlag from GswTblContent where FunctionId=103 and CustomerId=19";
            var db = new ZhongheRepo();
            db.Execute(strsql);
        }
        private void FillContentData(string sql,int num)
        {
            var strsql ="insert into GswTblHomeContent(Id, CreateDate, ContentTitle, ContentBrief, ContentDesc, Author, ContentPic, ContentThumb, CustomerId,ContentTypeId, FunctionId, NewFlag, ContentNo, RelateId, FileDesc, NewsY, NewsM, ReFlag)";
            strsql += " select top " + num + " Id, CreateDate, ContentTitle, ContentBrief, ContentDesc, Author, ContentPic, ContentThumb, CustomerId,ContentTypeId, FunctionId, NewFlag, ContentNo, RelateId, FileDesc, NewsY, NewsM, ReFlag from GswTblContent";
            strsql += sql;

            var db = new ZhongheRepo();
            db.Execute(strsql);
               // "select top 8 Id, CreateDate, ContentTitle, ContentBrief, ContentDesc, Author, ContentPic, ContentThumb, CustomerId,ContentTypeId, FunctionId, NewFlag, ContentNo, RelateId, FileDesc, NewsY, NewsM, ReFlag, 0 from GswTblContent where FunctionId=84 and CustomerId=19 and NewFlag=1 order by id desc";
           
            //var contentData = GswTblContent.Fetch(sql);
            //var homeContent = new GswTblHomeContent();
            //foreach (var item in contentData)
            //{
            //    homeContent.Author = item.Author;
            //    homeContent.ContentBrief = item.ContentBrief;
            //    homeContent.ContentDesc = item.ContentDesc;
            //    homeContent.ContentNo = item.ContentNo;
            //    homeContent.ContentThumb = item.ContentThumb;
            //    homeContent.ContentTitle = item.ContentTitle;
            //    homeContent.ContentTypeId = item.ContentTypeId;
            //    homeContent.CreateDate = item.CreateDate;
            //    homeContent.CustomerId = item.CustomerId;
            //    homeContent.FileDesc = item.FileDesc;
            //    homeContent.FunctionId = item.FunctionId;
            //    homeContent.NewFlag = item.NewFlag;
            //    homeContent.NewsM = item.NewsM;
            //    homeContent.NewsY = item.NewsY;
            //    homeContent.ReFlag = item.ReFlag;
            //    homeContent.RelateId = item.RelateId;
            //    homeContent.UpdateFlag = 0;
            //    homeContent.Id = item.Id;
            //    homeContent.Insert();
            //}
        }

        private void FillJobData(string sql,int num)
        {
            var strsql =
                "insert into GswTblHomeJobInfo(Id, PubDate, EndDate, JobNo, JobTitle, JobNum, WorkDate, WorkTime, Workplace, Salary, CustomerId, CustomerAlias, JobTypeId, JobTypeName, WorkTypeId, WorkTypeName, PayTypeId, PayTypeName, BodyLengthId, BodyLength, GenderId, GenderName, JobInterview, JobDesc, JobState, PubState, RegisterNum, DelFlag, ParentId, JobPic, ReFlag, HotFlag, AcceptedNum, PayNoticeDate, PayNoticeFlag, PayNotice)";
            strsql +=
                " select top " + num + " Id, PubDate, EndDate, JobNo, JobTitle, JobNum, WorkDate, WorkTime, Workplace, Salary, CustomerId, CustomerAlias, JobTypeId, JobTypeName, WorkTypeId, WorkTypeName, PayTypeId, PayTypeName, BodyLengthId, BodyLength, GenderId, GenderName, JobInterview, JobDesc, JobState, PubState, RegisterNum, DelFlag, ParentId, JobPic, ReFlag, HotFlag, AcceptedNum, PayNoticeDate, PayNoticeFlag, PayNotice from GswTblJobInfo";
            strsql += sql;
            var db = new ZhongheRepo();
            db.Execute(strsql);
            //var homeJob = new GswTblHomeJobInfo();
            //var jobData = GswTblJobInfo.Fetch(sql);
            //foreach (var item in jobData)
            //{
            //    homeJob.AcceptedNum = item.AcceptedNum;
            //    homeJob.BodyLength = item.BodyLength;
            //    homeJob.BodyLengthId = item.BodyLengthId;
            //    homeJob.CustomerAlias = item.CustomerAlias;
            //    homeJob.CustomerId = item.CustomerId;
            //    homeJob.DelFlag = item.DelFlag;
            //    homeJob.EndDate = item.EndDate;
            //    homeJob.GenderId = item.GenderId;
            //    homeJob.GenderName = item.GenderName;
            //    homeJob.HotFlag = item.HotFlag;
            //    homeJob.JobDesc = item.JobDesc;
            //    homeJob.JobInterview = item.JobInterview;
            //    homeJob.JobNo = item.JobNo;
            //    homeJob.JobNum = item.JobNum;
            //    homeJob.JobPic = item.JobPic;
            //    homeJob.JobState = item.JobState;
            //    homeJob.JobTitle = item.JobTitle;
            //    homeJob.JobTypeId = item.JobTypeId;
            //    homeJob.JobTypeName = item.JobTypeName;
            //    homeJob.ParentId = item.ParentId;
            //    homeJob.PayNotice = item.PayNotice;
            //    homeJob.PayNoticeDate = item.PayNoticeDate;
            //    homeJob.PayNoticeFlag = item.PayNoticeFlag;
            //    homeJob.PayTypeId = item.PayTypeId;
            //    homeJob.PayTypeName = item.PayTypeName;
            //    homeJob.PubDate = item.PubDate;
            //    homeJob.PubState = item.PubState;
            //    homeJob.ReFlag = item.ReFlag;
            //    homeJob.RegisterNum = item.RegisterNum;
            //    homeJob.Salary = item.Salary;
            //    homeJob.UpdateFlag = 0;
            //    homeJob.WorkDate = item.WorkDate;
            //    homeJob.WorkTime = item.WorkTime;
            //    homeJob.WorkTypeId = item.WorkTypeId;
            //    homeJob.WorkTypeName = item.WorkTypeName;
            //    homeJob.Workplace = item.Workplace;
            //    homeJob.Id = item.Id;
            //    homeJob.Insert();

            //}
        }
    }
}
